<template>
  <div>
    <button type="button" class="back-to-top" @click="backTop()" v-show="backTopShow"><i class="iconfont icon-icon-arrow-top"></i></button>
  </div>
</template>

<script type="text/ecmascript-6">
  export default {
    data() {
      return {
        backTopShow:false
      }
    },
    components: {},
    mounted() {
      window.addEventListener('scroll', this.handleScroll)
    },
    methods: {
      backTop() {
        let back = setInterval(() => {
          if(document.body.scrollTop||document.documentElement.scrollTop){
            document.body.scrollTop-=100;
            document.documentElement.scrollTop-=100;
          }else {
            clearInterval(back)
          }
        });
      },
      handleScroll(){
        if (document.documentElement.scrollTop + document.body.scrollTop > 100) {
          this.backTopShow=true;
        }
        else {
          this.backTopShow=false;
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
 .back-to-top {
    position: fixed;
    display: none;
    background: #18d26e;
    color: #fff;
    display: inline-block;
    width: 44px;
    height: 44px;
    text-align: center;
    line-height: 1;
    font-size: 16px;
    border-radius: 50%;
    right: 15px;
    bottom: 15px;
    //transition: background 0.5s;
    z-index: 11;
}
</style>
